<template>
  <div class="alummi-community-list news-list tab-default" ref="parent" @mouseenter="isHover = true" @mouseleave="isHover = false">
    <div class="tab-default__content mt-5 mt-md-13 container-content">
      <div
        class="card-list row"
        :class="[
          `row-cols-2`,
          `row-cols-md-2`,
          `row-cols-lg-4`,
        ]"
      >
        <div
          class="card-item col"
          v-for="card in pageList"
          :key="card.key"
        >
          <CardArticle
            :componentProps="card"
            :singleColumn="screenWidth < 768"
          />
        </div>
      </div>
    </div>
    <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>
  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { randomString } from '@/utils/common'
import { http } from '@/utils/http'
import CardArticle from '@/common/CardArticle/index.vue'
import Operate from '@/common/Operate/index.vue'
import Setting from './setting.vue'

defineOptions({
  name: 'AlummiCommunityList'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {}
  },

  // 是否预览
  isPreview: {
    type: Boolean,
    default: true
  },

  website: {
    type: String,
    default: 'ywies-tx'
  },

  locale: {
    type: String,
    default: 'sc'
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste', 'showLightboxVideo', 'showLightboxImage'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'AlummiCommunityList',
    api: props.modelValue?.api || '/component/production/articles',
    category: props.modelValue?.category || 'alumni-events',
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

handleInit()

const parent = ref<HTMLElement | null>(null)
const articleData = ref<any[]>([])
const screenWidth = ref(1024)
const pageList = computed(() => articleData.value)

const updateScreenWidth = () => {
  if (typeof window !== 'undefined') {
    screenWidth.value = window.innerWidth
  }
}

const getArticlesList = async () => {
  const config = {
    headers: {
      website: `${props.website}-${props.locale}`,
    },
  }
  const url = `/component/production/articles?category=${props.modelValue?.category}&per_page=4&page=1`
  const data = await http.get(url, config)
  articleData.value = data.data.data
}

onMounted(() => {
  getArticlesList()
  updateScreenWidth()
  if (typeof window !== 'undefined') {
    window.addEventListener('resize', updateScreenWidth)
  }
})

onUnmounted(() => {
  if (typeof window !== 'undefined') {
    window.removeEventListener('resize', updateScreenWidth)
  }
})
</script>

<style lang="scss" scoped>
.mt-5 {
  margin-top: 20px!important
}

.alummi-community-list {
  position: relative;
}

.row {
  --bs-gutter-x: 16px;
  --bs-gutter-y: 0;
  display: flex;
  flex-wrap: wrap;
  margin-top: calc(var(--bs-gutter-y)*-1);
  margin-right: calc(var(--bs-gutter-x)*-0.5);
  margin-left: calc(var(--bs-gutter-x)*-0.5)
}

.row>* {
  flex-shrink: 0;
  width: 100%;
  max-width: 100%;
  padding-right: calc(var(--bs-gutter-x)*0.5);
  padding-left: calc(var(--bs-gutter-x)*0.5);
  margin-top: var(--bs-gutter-y)
}

.col {
  flex: 1 0 0%
}

.row-cols-2>* {
  flex: 0 0 auto;
  width: 50%
}

@media (max-width: 767.98px) {
  .mt-md-13 {
    margin-top: 52px!important
  }

  .row-cols-md-2>* {
    flex: 0 0 auto;
    width: 50%
  }
}

@media (min-width: 1024px) { 
  .row-cols-lg-4>* {
    flex: 0 0 auto;
    width: 25%
  }
}

.card-list {
  --bs-gutter-x: 24px;
  --bs-gutter-y: 60px;
}

@media (max-width: 767.98px) {
  .card-list {
    --bs-gutter-x: 0;
    --bs-gutter-y: 40px;
    padding-left: 0;
    padding-right: 0;
  }
}

.row {
  --bs-gutter-x: 16px;
}

@media (max-width: 767.98px) {
  .row {
    --bs-gutter-x: 8px;
  }
}
</style>